package psn.zjk.springboot.web.servlet.i18n;

import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

/**
 * 自定义国际化
 * 张骏凯
 * 2020年1月31日
 */
public class MyLocaleResolver implements LocaleResolver {

    @Override public Locale resolveLocale(HttpServletRequest request) {
        String parameter = request.getParameter("l");
        Locale locale = Locale.getDefault();
        if (!StringUtils.isEmpty(parameter)) {
            String[] split = parameter.split("_");
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

    @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    }

}
